package com.android.music;

import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

import com.android.music.adapter.MyFragmentPagerAdapter;
import com.android.music.fragment.PlayingFragment;
import com.android.music.fragment.SettingsFragment;
import com.android.music.fragment.StorageFragment;
import com.android.music.fragment.UsbfunctionFragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;


public class MusicPlayer extends FragmentActivity implements OnClickListener
{
	private ViewPager mViewPager;
    private ToggleButton btn_coax;
	private ToggleButton btn_opti;
	private ToggleButton btn_usb;
	private ToggleButton btn_hdd;
	private ToggleButton btn_sd;
	private ToggleButton btn_flash;
	private ToggleButton btn_set;
	private ToggleButton btn_sort;
	private Fragment[] fragments;
	private ArrayList<Fragment> fragmentList;
	public int currIndex;


	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_player);
        
        initViewPager();
        
        initToggleButton();
    }



	private void initViewPager() {
		// TODO Auto-generated method stub
		mViewPager = (ViewPager)findViewById(R.id.viewpager);
		fragments = new Fragment[9];
		fragmentList = new ArrayList<Fragment>();		
		
		fragments[0] = new PlayingFragment();
		fragments[1] = new UsbfunctionFragment(0);
		fragments[2] = new UsbfunctionFragment(1);
		fragments[3] = new UsbfunctionFragment(2);
		fragments[4] = new StorageFragment(getResources().getString(R.string.hdd_path));
		fragments[5] = new StorageFragment(getResources().getString(R.string.sdcard_path));
		fragments[6] = new StorageFragment(getResources().getString(R.string.flash_path));
		fragments[7] = new SettingsFragment();
		fragments[8] = new PlayingFragment();

		for(int i = 0; i<9; i++)
			fragmentList.add(fragments[i]);
		
		mViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
		mViewPager.setCurrentItem(0);
		mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	private void initToggleButton() {
		// TODO Auto-generated method stub
        btn_coax = (ToggleButton)findViewById(R.id.id_btn_coax);
        btn_coax.setOnClickListener(this);
        btn_opti = (ToggleButton)findViewById(R.id.id_btn_opti);
        btn_opti.setOnClickListener(this);
        btn_usb = (ToggleButton)findViewById(R.id.id_btn_usb);
        btn_usb.setOnClickListener(this);
        btn_hdd = (ToggleButton)findViewById(R.id.id_btn_hdd);
        btn_hdd.setOnClickListener(this);
        btn_sd = (ToggleButton)findViewById(R.id.id_btn_sd);
        btn_sd.setOnClickListener(this);
        btn_flash = (ToggleButton)findViewById(R.id.id_btn_flash);
        btn_flash.setOnClickListener(this);
        btn_set = (ToggleButton)findViewById(R.id.id_btn_set);
        btn_set.setOnClickListener(this);
        btn_sort = (ToggleButton)findViewById(R.id.id_btn_sort);
        btn_sort.setOnClickListener(this);	
	}
	
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.id_btn_coax:
			currIndex = 1;
			setUsbFunction('B');
			break;
		case R.id.id_btn_opti:
			setUsbFunction('C');
			currIndex = 2;
			break;
		case R.id.id_btn_usb:
			setUsbFunction('D');
			currIndex = 3;
			break;
		case R.id.id_btn_hdd:
			setUsbFunction('A');
			currIndex = 4;
			break;
		case R.id.id_btn_sd:
			setUsbFunction('A');
			currIndex = 5;
			break;
		case R.id.id_btn_flash:
			setUsbFunction('A');
			currIndex = 6;
			break;
		case R.id.id_btn_set:
			setUsbFunction('A');
			currIndex = 7;
			break;
		case R.id.id_btn_sort:
			setUsbFunction('A');
			currIndex = 8;
			break;
		}
		mViewPager.setCurrentItem(currIndex);
	}
	
	public static void setUsbFunction(char ch) {
//		char buf[] = new char[1];
//		String path = "/sys/class/cs8416/state";
//		buf[0] = ch;
//		try {
//			FileWriter writer = new FileWriter(path);
//			writer.write(buf);
//			writer.close();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}
	
	public int getUsbFunction() {
		char buf[] = new char[1];
		String path = "/sys/class/cs8416/state";
		try {
			FileReader reader = new FileReader(path);
			reader.read(buf);
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		int usbIndex = 0;
		if(buf[0]=='A')
		{
			usbIndex = 0; 
		}
		else if(buf[0]=='B')
		{
			usbIndex = 1; 
		}
		else if(buf[0]=='C')
		{
			usbIndex = 2; 
		}
		else if(buf[0]=='D')
		{
			usbIndex = 3; 
		}
		
		return usbIndex;
	}
	
    public class MyOnPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
        	currIndex = arg0;
            switch (currIndex) {
            case 0:
            	setUsbFunction('A');
                btn_coax.setChecked(false);
                btn_opti.setChecked(false);
                btn_usb.setChecked(false);
                btn_hdd.setChecked(false);
                btn_sd.setChecked(false);
                btn_flash.setChecked(false);
                btn_set.setChecked(false);
                btn_sort.setChecked(false);	
                break;
            case 1:
            	setUsbFunction('B');
                btn_coax.setChecked(true);
                btn_opti.setChecked(false);
                btn_usb.setChecked(false);
                btn_hdd.setChecked(false);
                btn_sd.setChecked(false);
                btn_flash.setChecked(false);
                btn_set.setChecked(false);
                btn_sort.setChecked(false);	
                break;
            case 2:
            	setUsbFunction('C');
                btn_coax.setChecked(false);
                btn_opti.setChecked(true);
                btn_usb.setChecked(false);
                btn_hdd.setChecked(false);
                btn_sd.setChecked(false);
                btn_flash.setChecked(false);
                btn_set.setChecked(false);
                btn_sort.setChecked(false);	
                break;
            case 3:
            	setUsbFunction('D');
                btn_coax.setChecked(false);
                btn_opti.setChecked(false);
                btn_usb.setChecked(true);
                btn_hdd.setChecked(false);
                btn_sd.setChecked(false);
                btn_flash.setChecked(false);
                btn_set.setChecked(false);
                btn_sort.setChecked(false);	
                break;
            case 4:
            	setUsbFunction('A');
                btn_coax.setChecked(false);
                btn_opti.setChecked(false);
                btn_usb.setChecked(false);
                btn_hdd.setChecked(true);
                btn_sd.setChecked(false);
                btn_flash.setChecked(false);
                btn_set.setChecked(false);
                btn_sort.setChecked(false);	
                break; 
            case 5:
            	setUsbFunction('A');
                btn_coax.setChecked(false);
                btn_opti.setChecked(false);
                btn_usb.setChecked(false);
                btn_hdd.setChecked(false);
                btn_sd.setChecked(true);
                btn_flash.setChecked(false);
                btn_set.setChecked(false);
                btn_sort.setChecked(false);	
                break;  
            case 6:
            	setUsbFunction('A');
                btn_coax.setChecked(false);
                btn_opti.setChecked(false);
                btn_usb.setChecked(false);
                btn_hdd.setChecked(false);
                btn_sd.setChecked(false);
                btn_flash.setChecked(true);
                btn_set.setChecked(false);
                btn_sort.setChecked(false);	
                break;  
            case 7:
            	setUsbFunction('A');
                btn_coax.setChecked(false);
                btn_opti.setChecked(false);
                btn_usb.setChecked(false);
                btn_hdd.setChecked(false);
                btn_sd.setChecked(false);
                btn_flash.setChecked(false);
                btn_set.setChecked(true);
                btn_sort.setChecked(false);	
                break;  
            case 8:
            	setUsbFunction('A');
                btn_coax.setChecked(false);
                btn_opti.setChecked(false);
                btn_usb.setChecked(false);
                btn_hdd.setChecked(false);
                btn_sd.setChecked(false);
                btn_flash.setChecked(false);
                btn_set.setChecked(false);
                btn_sort.setChecked(true);	
                break;  
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }


}
